/**
 * @author Akos Szabo - Axi
 * Nov 26, 2011
 * 
 * Displays the Gantt diagramm of the tasks in a JTable
 * The table is synchronized with the TaskTablePanel JTable
 */
package com.axi.resplan.gui;

import javax.swing.*;
import javax.swing.table.TableColumn;

import com.axi.resplan.Task;
import com.axi.resplan.TimeTable;

import java.awt.*;
import java.text.*;
import java.util.*;

/**
 * @author Akos Szabo - Axi
 *
 */
public class TaskViewPanel extends JTable
{
	private TimeTable _tt;
	
	public TaskViewPanel(TimeTable argTimeTable)
	{		
		this(argTimeTable.getDateRangeWith()+20,argTimeTable.getTaskList().size());
		
		zoomTable(20);		
		this.addTimeTable(argTimeTable);
		this.updateView();
	}
	
	public void zoomTable(int argCellWith)
	{
		setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
		int maxi = this.getColumnModel().getColumnCount();
		for (int i = 0; i < maxi; i++) 
		{
		    this.getColumnModel().getColumn(i).setPreferredWidth( argCellWith );
		}
	}
	
	public TaskViewPanel( int argSizeX, int argSizeY)
	{
		super( argSizeY, argSizeX);
	}
	
	public void addTimeTable( TimeTable argTable)
	{
		_tt = argTable;
	}
	
		
	/**
	 * Update the view in the JTable
	 * 
	 */
	public void updateView()
	{
		//clear old content
		resetView();
		
		Format formatter = new SimpleDateFormat("yyyy.MM.dd."); //.HH.mm.ss
		//s = formatter.format(date);

		int range = _tt.getDateRangeWith();
		Date earliest = _tt.getEarliestDate();		
		//fill out the table with the data of _tt (TimeTable)
		Hashtable tl = _tt.getTaskList();
		Vector tv = _tt.getTaskVector();
		int i=0;
		//Enumeration en = tl.elements();
		Enumeration en = tv.elements();
		while(en.hasMoreElements())
		{
			//System.out.println(i);
			Task t = (Task)en.nextElement();
			
			int startPos = 10 + dateDayDiff(t.getEarliestStart(), earliest);
			int maxPos =  startPos + t.getLengthInHur()/8;
			
			for(int j = startPos; j<maxPos; j++ )
				setValueAt(""+t.getTaskId(), i, j); //setValueAt(""+i, i, j);

			
			//last commmand in this block is step to the next line
			i++;			
		}
	}
	
	private int dateDayDiff(Date arg1, Date arg2)
	{
		return (int)((arg2.getTime() - arg1.getTime()) / (1000 * 60 * 60 * 24));
	}
	
	private void resetView()
	{
		//use native JTable methodes
		this.selectAll();
		this.clearSelection();
	}		

}//end of class
